<?php
declare(strict_types=1);
/**
 * Created by PhpStorm
 * User: Administrator
 * Date: 2023/5/23
 * Time: 15:59:39
 */

namespace Service\LayoutService;

use App\Model\BbcModel\GoodsBrand;

class brandGroup extends \Service\BaseService
{
    /**
     * @param $mnid
     * @param $cid
     * @param $mn_sid
     * @param $mid
     * @param $moduleData
     * @return
     */
    public function getDataList($mnid,$cid,$mn_user,$mid,$moduleData,$extra){
        $mn_sid = $mn_user['id'];
        $sub_entry = $moduleData['setStyle']['sub_entry'] ?? [];
        if(count($sub_entry) <= 0){
            return [];
        }
        $brand_ids = array_column($sub_entry,'id');

        $brand_list = GoodsBrand::instance()
            ->where([
                ['cid','=',$cid],
                ['df','=',0],
            ])
            ->whereIn('id',$brand_ids)
            ->select('id,name,pic,banner_image')
            ->get()->toArray();
        if(count($brand_list) <= 0){
            return [];
        }

        $brand_list_ids = array_column($brand_list,'id');
        $brand_list_map = array_column($brand_list,null,'id');

        foreach ($sub_entry as $k=>$v){
            if(!in_array($v['id'],$brand_list_ids)){ //品牌不正常
                unset($sub_entry[$k]);
                continue;
            }

            $sub_entry[$k]['title'] = $brand_list_map[$v['id']]['name'] ?? '';
            $sub_entry[$k]['logo'] = $brand_list_map[$v['id']]['pic'] ?? '';
            $sub_entry[$k]['banner_image'] = $brand_list_map[$v['id']]['banner_image'] ?? '';
        }
        if(!$sub_entry){
            return [];
        }
        $sub_entry = array_values($sub_entry);
        unset($moduleData['setStyle']['sub_entry']);
        $moduleData['setStyle']['sub_entry'] = $sub_entry;
        return $moduleData;
    }
}